<?php

namespace app;

use XRsa\XRsa;

class RsaKey
{
    protected $publicKey = '-----BEGIN PUBLIC KEY-----
MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAMBQCjIW4J7EEMeT9GtQbhBTgjyirXi+
6vsdWoxHiwOF5igNjb+6PfwBUuTP/AtNyeyOvQTXKCHaaqXg864P2dMCAwEAAQ==
-----END PUBLIC KEY-----';

    protected $privateKey = '-----BEGIN PRIVATE KEY-----
MIIBVQIBADANBgkqhkiG9w0BAQEFAASCAT8wggE7AgEAAkEAwFAKMhbgnsQQx5P0
a1BuEFOCPKKteL7q+x1ajEeLA4XmKA2Nv7o9/AFS5M/8C03J7I69BNcoIdpqpeDz
rg/Z0wIDAQABAkBSA5BljiYa70ispGdvQKy/n62CCGpsfRcfIoSUIIqjZD/NpeZI
CvzIlBerSVXUmZAd7N3eLZywN0Kg1bL+dW1xAiEA9vc87CjfzHyD3tc83XX6sq7M
a58cQ661CNGjykbfETkCIQDHWP3OhHCuAolnfehBnn+zSbGvpg2YWEELnlwH7Gvf
awIhANCao7YjDG7QWh8XlYzGw9y8M2VQf3JYFwh886m/VVpZAiB3IJsr2sFre2CZ
VAiDjjBJ9/QTP2xIahUU2GGu6n3cWQIhAN9XSpbxdk3fPoRwOEUT87/Km/lHFwz3
3k1+DRaJzGL/
-----END PRIVATE KEY-----
';

    protected $rsa = null;

    public function encryption($userId)
    {
        return $this->getRseClient()->publicEncrypt($userId);
    }

    public function decrypt($data)
    {
        return $this->getRseClient()->privateDecrypt($data);
    }

    /**
     * @return XRsa|null
     */
    protected function getRseClient()
    {
        if (empty($this->rsa)) {
            return $this->rsa = new XRsa($this->publicKey, $this->privateKey);
        }

        return $this->rsa;
    }
}
